import Common from '../common'
var Js = Common('staff')
export default {
  name: 'staff',
  mixins: [Js],
  components: {},
  data() {
    return {
      fields: [{
          key: 'id',
          label: 'ID',
          application: [],
          componentName: 'sls-input',
          listType: '',
          width: 50
        },
        {
          key: 'user_no',
          label: '用户NO',
          application: [],
          componentName: 'sls-input',
          listType: 'normal',
          header_search: {
            componentName: 'sls-input',
            placeholder: '请输入用户NO',
            clearable: true,
            changeFunc: function(val, self) {
              if (val) {
                self.searchItem.user_no = ['LIKE', ['%' + val + '%']];
              } else {
                delete self.searchItem.user_no;
              };
            },
          },
        },
        {
          key: "login_name",
          label: '登录账号',
          application: ['添加账号', '编辑账号'],
          componentName: 'sls-input',
          listType: 'normal',
          header_search: {
            componentName: 'sls-input',
            style: 'width:160px;margin-right:2px;',
            placeholder: '请输入登录账号',
            changeFunc: function(e, self) {
              if (e) {
                self.searchItem.login_name = ['LIKE', ['%' + e + '%']]
              } else {
                delete self.searchItem.login_name
              };
            },
          },
        },
        {
          key: 'password',
          label: '登录密码',
          application: ['编辑账号', '添加账号'],
          componentName: 'sls-input',
          dialog: {
            type: 'password'
          },
          header_search: {
            componentName: 'sls-date-time-range',
            style: 'width:160px;margin-right:2px;',
            start_placeholder: '创建开始时间',
            end_placeholder: '创建结束时间',
            changeFunc: function(value, self) {
              if (!value) {
                delete self.searchItem.create_time;
              } else {
                self.searchItem.create_time = ['between', value = value.map(function(e) {
                  return e / 1000;
                })]
              };

            },
          },
        },
        {
          key: 'mainImg',
          label: '头像',
          application: ['添加账号', '编辑账号'],
          componentName: 'upload',
          needStr: true,
          dialogStyle: 'width:100%;',
          customSlot: 'mainImg',
          listType: 'normal',
        },
        {
          key: "orderPrice",
          label: '业绩',
          componentName: 'sls-input',
          listType: 'normal',
          header_search: {
            componentName: 'sls-date-time-range',
            start_placeholder: '业绩筛选开始时间',
            end_placeholder: '业绩筛选结束时间',
            changeFunc: function(value, self) {
              if (!value) {
                delete self.orderSearchItem.create_time;
              } else {
                self.orderSearchItem.create_time = ['between', value = value.map(function(e) {
                  return e / 1000;
                })]
              };
            },
          },
        },
        {
          key: 'type',
          label: '流水类型',
          application: ['添加流水'],
          componentName: 'sls-select',
          optionsName: 'typeOptions',
          listType: '',
          limit: 10,
          defaultProps: {
            label: 'text',
            value: 'value',
          },
          placeholder: '请选择流水类型',
          width: 50,
        },
        {
          key: "count",
          label: '添加流水',
          application: ['添加流水'],
          componentName: 'sls-input',
        },
        {
          key: "trade_ifno",
          label: '流水说明',
          application: ['添加流水'],
          componentName: 'sls-input',
        },
        {
          key: 'create_time',
          label: '创建时间',
          listType: 'normal',
          placeholder: '请选择创建时间',
          width: 200,
        },
        {
          label: '操作',
          listType: 'deal',
          width: 200
        },
      ],
      // 按钮配置
      btn_info: [

        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'header',
          text: function(data) {
            return '添加账号'
          },
          func: {
            apiName: function(self) {
              return "api_registerUser"
            },
            formData: function(self) {
              var data = {
                login_name: '',
                password: '',
                headImgUrl: []
              };
              return data
            },
            postData: function(self) {
              if (!self.submitData.login_name || !self.submitData.password) {
                self.$$notify('请输入登录账号及密码', 'fail');
                return;
              }
              var postData = {
                user: self.submitData,
                thirdapp_id: 2,
                role: 12,
                config_id: 2,
                login_name: self.submitData.login_name,
                password: self.submitData.password,
              };
              delete postData.user.login_name;
              return postData;
            }
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          text: function(data) {
            return '编辑账号'
          },
          func: {
            apiName: function(self) {
              return "api_userUpdate"
            },
            formData: function(self) {
              return self.formData
            },
            postData: function(self) {
              var postData = {
                searchItem: {
                  id: self.formData.id,
                  user_no: self.formData.user_no,
                },
                data: self.submitData
              };


              return postData;
            }
          },
        },
        {
          type: 'danger',
          icon: 'delete',
          size: 'mini',
          funcType: 'submit',
          position: 'header',
          text: function(data) {
            return '删除选中'
          },
          func: {
            apiName: function(self) {
              return "api_userUpdate"
            },
            postData: function(self) {
              var deleteArray = [];
              for (var i = 0; i < self.selectionArray.length; i++) {
                deleteArray.push(self.selectionArray[i].id);
              };
              var postData = {
                searchItem: {
                  id: ['in', deleteArray],
                  user_type: 0
                },
                data: {
                  status: -1
                }
              };
              return postData;
            }
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          text: function(data) {
            return '添加流水'
          },
          styleFunc: function(data) {
            return 'background-color: #2ac1ce;border-color:#2ac1ce'
          },
          func: {
            apiName: function(self) {
              return "api_flowLogAdd"
            },
            formData: function(self) {
              var data = self.formData;
              return data
            },
            postData: function(self) {
              if (!self.submitData.count) {
                self.$message({
                  message: '请输入流水金额',
                  type: 'warning'
                });
                return
              };
              self.submitData.user_no = self.formData.user_no;
              self.submitData.thirdapp_id = self.formData.thirdapp_id;
              self.submitData.role = self.formData.role;
              self.submitData.user_type = self.formData.user_type;

              var postData = {
                data: self.submitData
              };

              return postData;
            }
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          text: function(data) {
            return '管理权限'
          },
          isHide: function(self) {
            if (self.formData.user_no == self.$store.getters.getUserinfo.user_no && self.$store.getters.getUserinfo
              .primary_scope <
              60) {
              return true;
            } else {
              return false;
            };
          },
          funcType: 'func',
          func: {
            func: function(self) {
              console.log('self.originFormData', self.originFormData);
              self.$router.push({
                path: '/home/access',
                name: '权限管理',
                params: {
                  defaultChecked: self.originFormData.auth,
                  role: self.originFormData.role,
                  user_no: self.originFormData.user_no,
                  behavior: 0,
                  primary_scope: self.originFormData.primary_scope,
                  path: '/home/personal'
                }
              });
            },
            postData: function(self) {
              var postData = {
                searchItem: {
                  id: self.formData.id,
                },
                data: data
              };
              return postData;
            },
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          text: function(data) {
            return '推广成员'
          },
          funcType: 'func',
          func: {
            func: function(self) {
              self.$router.push({
                path: '/home/productdetail',
                name: '推广成员',
                params: {
                  parent_no: self.originFormData.user_no,
                  delKeepAlive: true,
                  backPath: '/home/staff'
                }
              });
            },
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          text: function(data) {
            return '推广订单'
          },
          styleFunc: function(data) {
            return 'background-color: #396edf;border-color:#396edf'
          },
          funcType: 'func',
          func: {
            func: function(self) {
              self.$router.push({
                path: '/home/xorder',
                name: '订单列表',
                params: {
                  type: 1,
                  user_no: self.originFormData.user_no,
                  delKeepAlive: true,
                  backPath: '/home/staff'
                }
              });
            },
          },
        }
      ],


      searchItem: {
        role: 12
      },
      startSearchItem: {
        role: 12
      },
      optionData: {
        labelOptions: [],
        statusOptions: [{
          text: '启用',
          value: 1
        }, {
          text: '待审核',
          value: 2
        }, {
          text: '禁用',
          value: -1
        }],
        levelOptions: [{
            text: '启用',
            value: 1
          },
          {
            text: '禁用',
            value: -1
          },
        ],
        roleOptions: [],
        typeOptions: [{
            text: '奖金',
            value: 2
          },
          {
            text: '积分',
            value: 3
          },
        ],
      },
      otherData: {},
      UserInfo: {
        tableName: 'UserInfo',
        searchItem: {},
        key: 'user_no',
        middleKey: 'user_no',
        condition: 'in',
      },
      orderSearchItem: {
        pay_status: 1,
        status: 1,
        level: 0,
        order_type: 1
      },
    }

  },

  mounted() {
    const self = this;
    self.searchData = self.$$cloneForm(self.searchItem);
    this.init()
  },
  computed: {
    token: function() {
      return this.$store.getters.getToken
    }
  },
  watch: {

  },
  methods: {

    /**
     * 初始化
     */
    init() {
      this.initMainData();
      this.initLevelData();
    },

    async excelOutput(self) {

      if (self.btnName == '导出') {
        const postData = {};
        postData.token = self.$store.getters.getToken;
        if (self.searchItem) {
          postData.searchItem = self.$$cloneForm(self.searchItem)
        };
        postData.excelOutput = {
          expTitle: 'test',
          expCellName: [



            {
              title: '姓名',
              key: ['info', 'name'],
              type: 'string',
            },
            {
              title: '地址',
              key: ['info', 'address'],
              type: 'string',
            },
            {
              title: '电话',
              key: ['info', 'phone'],
              type: 'string',
            },
            {
              title: '性别(1：男 2：女)',
              key: ['info', 'gender'],
              type: 'string',
            },
            {
              title: '年龄',
              key: ['info', 'age'],
              type: 'string',
            },
            {
              title: '购物次数',
              key: ['order', 'count'],
              type: 'string',
            },
            {
              title: '消费金额',
              key: ['order', 'price'],
              type: 'string',
            },
            {
              title: '消费时间跨度',
              key: ['orderDay'],
              type: 'string',
            },
          ],
          fileName: '用户列表'
        };

        window.open('http://42.193.100.244/155/public/index.php/api/v1/Base/User/get?token=' +
          postData.token +
          '&searchItem=' +
          JSON.stringify(postData.searchItem) +
          '&excelOutput=' +
          JSON.stringify(postData.excelOutput))
      }
    },

    async initLevelData() {

      const self = this;
      const postData = {};

      postData.token = self.$store.getters.getToken;
      postData.searchItem = {
        menu_id: 21
      };
      var res = await self.$$api_articleGet({
        data: postData
      });
      self.optionData.levelOptions = res.info.data;
      self.optionData.levelOptions.push({
        title: '普通会员',
        listorder: 0
      })
    },

    /**
     * 列表主函数
     */
    async initMainData() {

      const self = this;
      const postData = {};
      postData.paginate = self.$$cloneForm(self.paginate);
      postData.token = self.$store.getters.getToken;
      if (self.searchItem) {
        postData.searchItem = self.$$cloneForm(self.searchItem)
      };
      if (JSON.stringify(self.getBefore) != "{}") {
        postData.getBefore = self.$$cloneForm(self.getBefore);
      };
      postData.getAfterCompute = {
        orderPrice: {
          tableName: 'Order',
          field: 'price',
          method: 'sum',
          condition: '=',
          key: 'passage1',
          middleKey: 'user_no',
          searchItem: self.orderSearchItem,
        },
      };
      var res = await self.$$api_userGet({
        data: postData
      });

      res.info.data.forEach((item, index) => {
        item.headImgUrl = item.headImgUrl ? [{
          'url': item.headImgUrl,
          type: 'image'
        }] : [];
      });
      self.mainData = res.info.data;


      self.paginate.count = res.info.total;

    },

  },


}
